package hust.music.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hust.music.dao.JpaDao;
import hust.music.entity.*;
import hust.music.service.HomePageModService;
import hust.music.util.DateConvert;
import hust.music.util.DescComparator;
import hust.music.util.DynamicInfo;

/**
 * HomePageServiceImpl
 * 
 * @author Administrator
 * @since 1.0
 */
@Service("homePageModService")
@Transactional
public class HomePageModServiceImpl implements HomePageModService {

	@Autowired
	private JpaDao jpaDao;

	/**
	 * 获取用户关注的人的动态信息
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户关注的人的动态信息
	 */
	@Override
	public List<DynamicInfo> getFollowDynamicInfo(User user, int firstResult,
			int maxResult) {
		jpaDao.getEntityManager().createNativeQuery(
				"delete from dynamic where i_id = ?").setParameter(1,
				user.getId()).executeUpdate();

		String query = "insert into dynamic(d_id, time, type, i_id) "
				+ "select *, :id as i_id from ( select m.id as d_id, m.time, 'music' as type from musics m where m.user_id in "
				+ "( select u.id from users u cross join follow_rel f where f.user_id = :id and f.follower_id = u.id )"
				+ " union "
				+ "select c.id as d_id, c.time, 'comment' as type from comments c where c.user_id in "
				+ "( select u.id from users u cross join follow_rel f where f.user_id = :id and f.follower_id = u.id )"
				+ " union "
				+ "select f.id as d_id, f.time, 'follow' as type from follow_rel f where f.user_id in "
				+ "( select u.id from users u cross join follow_rel f where f.user_id = :id and f.follower_id = u.id ) ) "
				+ "as p order by p.time desc limit :firstResult, :maxResult";
		jpaDao.getEntityManager().createNativeQuery(query).setParameter("id",
				user.getId()).setParameter("firstResult", firstResult)
				.setParameter("maxResult", maxResult).executeUpdate();

		Map<String, DynamicInfo> map = new HashMap<String, DynamicInfo>();

		List<Music> musics = jpaDao
				.findAll(
						Music.class,
						"from Music m where m.id in "
								+ "(select d.d_id from Dynamic d where d.i_id = ? and d.type = ? )",
						new Object[] { user.getId(), "music" });
		for (Music music : musics) {
			DynamicInfo dynamicInfo = new DynamicInfo(music.getUser().getId(),
					music.getUser().getNickname(), music.getUser().getAvatar(),
					music.getId(), music.getTitle(), music.getImage(), null,
					null, music.getPath(), DateConvert
							.convertDateToString(music.getUploadTime()),
					"music", music.getDescription());
			map.put(DateConvert.convertDateToString(music.getUploadTime()),
					dynamicInfo);
		}

		List<Comment> comments = jpaDao
				.findAll(
						Comment.class,
						"from Comment c where c.id in "
								+ "(select d.d_id from Dynamic d where d.i_id = ? and d.type = ? )",
						new Object[] { user.getId(), "comment" });
		for (Comment comment : comments) {
			DynamicInfo dynamicInfo = new DynamicInfo(
					comment.getUser().getId(), comment.getUser().getNickname(),
					comment.getUser().getAvatar(), comment.getMusic().getId(),
					comment.getMusic().getTitle(), comment.getMusic()
							.getImage(), comment.getMusic().getUser().getId(),
					comment.getMusic().getUser().getNickname(), comment
							.getContent(), DateConvert
							.convertDateToString(comment.getTime()), "comment",
					comment.getMusic().getPath());
			map.put(DateConvert.convertDateToString(comment.getTime()),
					dynamicInfo);
		}

		List<FollowRel> follows = jpaDao
				.findAll(
						FollowRel.class,
						"from FollowRel f where f.id in "
								+ "(select d.d_id from Dynamic d where d.i_id = ? and d.type = ? )",
						new Object[] { user.getId(), "follow" });
		for (FollowRel follow : follows) {
			DynamicInfo dynamicInfo = new DynamicInfo(follow
					.getInitiativeFollow().getId(), follow
					.getInitiativeFollow().getNickname(), follow
					.getInitiativeFollow().getAvatar(), follow
					.getPassiveFollow().getId(), follow.getPassiveFollow()
					.getNickname(), null, null, null, null, DateConvert
					.convertDateToString(follow.getTime()), "follow", null);
			map.put(DateConvert.convertDateToString(follow.getTime()),
					dynamicInfo);
		}

		Map<String, DynamicInfo> treeMap = new TreeMap<String, DynamicInfo>(
				new DescComparator());
		treeMap.putAll(map);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>(treeMap.values());

		return list;
	}

	/**
	 * 获取用户关注的人的动态信息总数
	 * 
	 * @param user 用户
	 * @return 用户关注的人的动态信息总数
	 */
	@Override
	public int getTotalFollowDynamicInfo(User user) {
		String query = "select m.time from musics m where m.user_id in "
				+ "( select u.id from users u cross join follow_rel f where f.user_id = :id and f.follower_id = u.id )"
				+ " union "
				+ "select c.time from comments c where c.user_id in "
				+ "( select u.id from users u cross join follow_rel f where f.user_id = :id and f.follower_id = u.id )"
				+ " union "
				+ "select f.time from follow_rel f where f.user_id in "
				+ "( select u.id from users u cross join follow_rel f where f.user_id = :id and f.follower_id = u.id )";

		return jpaDao.getEntityManager().createNativeQuery(query).setParameter(
				"id", user.getId()).getResultList().size();
	}

	/**
	 * 获取关注粉丝分享数
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 关注粉丝分享数
	 */
	@Override
	public Map<String, Integer> getPersonalCount(User user) {
		Map<String, Integer> map = new HashMap<String, Integer>();

		List<Music> shares = jpaDao.findAll(Music.class,
				"select m from Music m where m.user = ?", user);
		map.put("share", Integer.valueOf(shares.size()));

		List<FollowRel> follows = jpaDao.findAll(FollowRel.class,
				"select f from FollowRel f where f.initiativeFollow = ?", user);
		map.put("follow", Integer.valueOf(follows.size()));

		List<FollowRel> fans = jpaDao.findAll(FollowRel.class,
				"select f from FollowRel f where f.passiveFollow = ?", user);
		map.put("fan", Integer.valueOf(fans.size()));

		return map;
	}

	/**
	 * 获取用户流派集
	 * 
	 * @param user 用户
	 * @return 用户流派集
	 * @throws Exception
	 */
	@Override
	public List<DynamicInfo> getPlayList(User user) throws Exception {
		List<PlayList> list = jpaDao
				.findAll(
						PlayList.class,
						"select p from PlayList p where p.user = ? order by p.time desc",
						user);

		List<DynamicInfo> playList = new ArrayList<DynamicInfo>();
		for (PlayList p : list) {
			DynamicInfo dynamic = new DynamicInfo(p.getId(), p.getTitle(), p
					.getAvatar(), null, p.getDescription(), null, null, null, null, null,
					null, null);
			playList.add(dynamic);
		}

		return playList;
	}

	/**
	 * 获取用户留言
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户留言
	 * @throws Exception
	 */
	@Override
	public List<DynamicInfo> getMessage(User user, int firstResult,
			int maxResult) throws Exception {
		List<Message> list = jpaDao
				.findByPage(
						Message.class,
						"select m from Message m where m.passiveUser = ? order by m.time desc",
						user, firstResult, maxResult);

		List<DynamicInfo> messages = new ArrayList<DynamicInfo>();
		for (Message m : list) {
			DynamicInfo dynamic = new DynamicInfo(
					m.getInitiativeUser().getId(), m.getInitiativeUser()
							.getNickname(), m.getInitiativeUser().getAvatar(),
					null, null, null, null, null, m.getContent(), DateConvert
							.convertDateToString(m.getTime()), null, null);
			messages.add(dynamic);
		}

		return messages;
	}

	/**
	 * 获取用户留言总数
	 * 
	 * @param user 用户
	 * @return 用户留言总数
	 */
	@Override
	public int getTotalMessage(User user) {
		return jpaDao
				.findAll(
						Message.class,
						"select m from Message m where m.passiveUser = ? order by m.time desc",
						user).size();
	}

	/**
	 * 获取用户关注的人
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户关注的人
	 * @throws Exception
	 */
	@Override
	public List<DynamicInfo> getFollow(User user, int firstResult, int maxResult)
			throws Exception {
		List<User> list = jpaDao.findByPage(User.class,
				"select u from User u, FollowRel f where f.initiativeFollow = ? "
						+ "and f.passiveFollow = u order by f.time desc", user,
				firstResult, maxResult);

		List<DynamicInfo> follows = new ArrayList<DynamicInfo>();
		for (User u : list) {
			DynamicInfo dynamic = new DynamicInfo(u.getId(), u.getNickname(), u
					.getAvatar(), null, null, null, null, null, null, null,
					null, null);
			follows.add(dynamic);
		}

		return follows;
	}

	/**
	 * 获取用户好友
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户好友
	 * @throws Exception
	 */
	@Override
	public List<DynamicInfo> getFriend(User user, int firstResult, int maxResult)
			throws Exception {
		List<User> list = jpaDao.findByPage(User.class,
				"select u from User u, FriendRel f where ( f.user = ? "
						+ "and f.friend = u ) or ( f.friend = ? "
						+ "and f.user = u ) order by f.time desc",
				new Object[] { user, user }, firstResult, maxResult);

		List<DynamicInfo> follows = new ArrayList<DynamicInfo>();
		for (User u : list) {
			DynamicInfo dynamic = new DynamicInfo(u.getId(), u.getNickname(), u
					.getAvatar(), null, null, null, null, null, null, null,
					null, null);
			follows.add(dynamic);
		}

		return follows;
	}

	/**
	 * 获取用户提醒
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户提醒
	 */
	@Override
	public Map<String, DynamicInfo> getRemind(User user, int firstResult,
			int maxResult) {
		jpaDao.getEntityManager().createNativeQuery(
				"delete from remind where i_id = ?").setParameter(1,
				user.getId()).executeUpdate();

		String query = "insert into remind(r_id, time, type, i_id) select *, :id as i_id from ( "
				+ "select f.id as r_id, f.time, 'apply' as type from friend_rel f where f.user_id = :id and f.ispassed = 0 and f.isread = 0"
				+ " union "
				+ "select f.id as r_id, f.time, 'friend' as type from friend_rel f where f.apply_id = :id and f.ispassed = 1 and f.isread = 0"
				+ " union "
				+ "select f.id as r_id, f.time, 'follow' as type from follow_rel f where f.follower_id = :id and f.isread = 0"
				+ " union "
				+ "select m.id as r_id, m.time, 'message' as type from messages m where m.passive_user = :id and m.isread = 0"
				+ " union "
				+ "select c.id as r_id, c.time, 'comment' as type from comments c, musics m where c.music_id = m.id and m.user_id = :id and c.isread = 0"
				+ ") as r order by r.time desc limit :firstResult, :maxResult";
		jpaDao.getEntityManager().createNativeQuery(query).setParameter("id",
				user.getId()).setParameter("firstResult", firstResult)
				.setParameter("maxResult", maxResult).executeUpdate();

		Map<String, DynamicInfo> map = new HashMap<String, DynamicInfo>();

		List<FriendRel> applys = jpaDao
				.findAll(
						FriendRel.class,
						"from FriendRel f where f.id in "
								+ "(select r.r_id from Remind r where r.i_id = ? and r.type = ? )",
						new Object[] { user.getId(), "apply" });
		for (FriendRel f : applys) {
			DynamicInfo dynamicInfo = new DynamicInfo(f.getFriend().getId(), f
					.getFriend().getNickname(), f.getFriend().getAvatar(),
					f.getId(), null, null, null, null, null, null, "apply", null);
			map.put(DateConvert.convertDateToString(f.getTime()), dynamicInfo);
		}

		List<FriendRel> friends = jpaDao
				.findAll(
						FriendRel.class,
						"from FriendRel f where f.id in "
								+ "(select r.r_id from Remind r where r.i_id = ? and r.type = ? )",
						new Object[] { user.getId(), "friend" });
		for (FriendRel f : friends) {
			DynamicInfo dynamicInfo = new DynamicInfo(f.getUser().getId(), f
					.getUser().getNickname(), f.getUser().getAvatar(), null,
					null, null, null, null, null, null, "friend", null);
			map.put(DateConvert.convertDateToString(f.getTime()), dynamicInfo);
			f.setIsRead(1);
		}

		List<FollowRel> follows = jpaDao
				.findAll(
						FollowRel.class,
						"from FollowRel f where f.id in "
								+ "(select r.r_id from Remind r where r.i_id = ? and r.type = ? )",
						new Object[] { user.getId(), "follow" });
		for (FollowRel f : follows) {
			DynamicInfo dynamicInfo = new DynamicInfo(f.getInitiativeFollow()
					.getId(), f.getInitiativeFollow().getNickname(), f
					.getInitiativeFollow().getAvatar(), null, null, null, null,
					null, null, null, "follow", null);
			map.put(DateConvert.convertDateToString(f.getTime()), dynamicInfo);
			f.setIsRead(1);
		}

		List<Message> messages = jpaDao
				.findAll(
						Message.class,
						"from Message m where m.id in "
								+ "(select r.r_id from Remind r where r.i_id = ? and r.type = ? )",
						new Object[] { user.getId(), "message" });
		for (Message m : messages) {
			DynamicInfo dynamicInfo = new DynamicInfo(m.getInitiativeUser()
					.getId(), m.getInitiativeUser().getNickname(), m
					.getInitiativeUser().getAvatar(), null, null, null, null,
					null, m.getContent(), null, "mesage", null);
			map.put(DateConvert.convertDateToString(m.getTime()), dynamicInfo);
			m.setIsRead(1);
		}

		List<Comment> comments = jpaDao
				.findAll(
						Comment.class,
						"from Comment c where c.id in "
								+ "(select r.r_id from Remind r where r.i_id = ? and r.type = ? )",
						new Object[] { user.getId(), "comment" });
		for (Comment c : comments) {
			DynamicInfo dynamicInfo = new DynamicInfo(c.getUser().getId(), c
					.getUser().getNickname(), c.getUser().getAvatar(), c
					.getMusic().getId(), c.getMusic().getTitle(), null, null,
					null, c.getContent(), null, "comment", null);
			map.put(DateConvert.convertDateToString(c.getTime()), dynamicInfo);
			c.setIsRead(1);
		}

		return map;
	}

	/**
	 * 获取用户最近上传的音乐
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户最近上传的音乐
	 */
	@Override
	public List<DynamicInfo> getRecentUploadMusic(User user, int firstResult,
			int maxResult) {
		List<Music> list = jpaDao
				.findByPage(
						Music.class,
						"select m from Music m where m.user = ? order by m.uploadTime desc",
						user, firstResult, maxResult);

		List<DynamicInfo> music = new ArrayList<DynamicInfo>();
		for (Music m : list) {
			DynamicInfo dynamic = new DynamicInfo(m.getId(), m.getTitle(), m
					.getImage(), null, m.getPath(), m.getDescription(), null, null, null,
					DateConvert.convertDateToString(m.getUploadTime()), null,
					null);
			music.add(dynamic);
		}

		return music;
	}

	/**
	 * 获取用户好友粉丝榜
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户好友粉丝榜
	 */
	@Override
	public List<DynamicInfo> getFanRank(User user, int firstResult,
			int maxResult) {
		List<User> list = jpaDao
				.findByPage(
						User.class,
						"select u from User u, FriendRel f, FollowRel l where (( f.user = u and f.friend = ? ) "
								+ "or ( f.user = ? and f.friend = u )) and l.passiveFollow = u "
								+ "group by u.id order by count(u.id) desc",
						new Object[] { user, user }, firstResult, maxResult);

		List<DynamicInfo> users = new ArrayList<DynamicInfo>();

		for (User u : list) {
			int fans = jpaDao
					.findAll(
							User.class,
							"select u from User u, FollowRel f where f.initiativeFollow = u and f.passiveFollow = ?",
							u).size();
			DynamicInfo dynamic = new DynamicInfo(u.getId(), u.getNickname(), u
					.getAvatar(), null, null, null, null, null, null, null,
					null, "" + fans);
			users.add(dynamic);
		}

		return users;
	}

	/**
	 * 获取用户音乐收听榜
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户音乐收听榜
	 */
	@Override
	public List<DynamicInfo> getMusicListeningRank(User user, int firstResult,
			int maxResult) {
		List<Music> list = jpaDao
				.findByPage(
						Music.class,
						"select m from Music m where m.user = ? order by m.listenTimes desc",
						user, firstResult, maxResult);

		List<DynamicInfo> music = new ArrayList<DynamicInfo>();
		for (Music m : list) {
			DynamicInfo dynamic = new DynamicInfo(m.getId(), m.getTitle(), m
					.getImage(), null, null, null, null, null, null, null,
					null, "" + m.getListenTimes());
			music.add(dynamic);
		}

		return music;
	}

	/**
	 * 是否关注
	 * 
	 * @param user 用户1
	 * @param home 用户2
	 * @return number
	 */
	@Override
	public int getIsFollow(User user, User home) {
		List<FollowRel> list = jpaDao
				.findAll(
						FollowRel.class,
						"select f from FollowRel f where f.initiativeFollow = ? and f.passiveFollow = ?",
						new Object[] { user, home });
		if (list.size() == 1) {
			return 1;
		}
		return 0;
	}

	/**
	 * 是否好友
	 * 
	 * @param user 用户1
	 * @param home 用户2
	 * @return number
	 */
	@Override
	public int getIsFriend(User user, User home) {
		List<FriendRel> list = jpaDao
				.findAll(
						FriendRel.class,
						"select f from FriendRel f where (f.user = ? and f.friend = ?) "
								+ "or (f.friend = ? and f.user = ?) and f.isPassed = 1",
						new Object[] { user, home, user, home });
		if (list.size() == 1) {
			return 1;
		}
		return 0;
	}

	/**
	 * 添加或删除关注
	 * 
	 * @param user 用户1
	 * @param home 用户2
	 * @return number
	 */
	@Override
	public int updateFollow(User user, User home) {
		List<FollowRel> list = jpaDao
				.findAll(
						FollowRel.class,
						"select f from FollowRel f where f.initiativeFollow = ? and f.passiveFollow = ?",
						new Object[] { user, home });
		if (list.size() == 1) {
			jpaDao
					.getEntityManager()
					.createNativeQuery(
							"delete from follow_rel where user_id = ? and follower_id = ?")
					.setParameter(1, user.getId())
					.setParameter(2, home.getId()).executeUpdate();
			return 0;
		} else {
			FollowRel f = new FollowRel();
			f.setTime(new Date());
			User u = jpaDao.get(User.class, user.getId());
			User h = jpaDao.get(User.class, home.getId());
			f.setInitiativeFollow(u);
			f.setPassiveFollow(h);
			f.setIsRead(0);
			jpaDao.save(f);
			return 1;
		}
	}

	/**
	 * 申请或删除好友
	 * 
	 * @param user 用户1
	 * @param home 用户2
	 * @param isDelete 是否删除好友标识
	 * @return number
	 */
	@Override
	public int updateFriend(User user, User home, int isDelete) {
		if (isDelete == 1) {
			jpaDao
					.getEntityManager()
					.createNativeQuery(
							"delete from friend_rel where (user_id = :user and apply_id = :home) "
									+ "or (user_id = :home and apply_id = :user) and isPassed = 1")
					.setParameter("user", user).setParameter("home", home)
					.executeUpdate();
			return 0;
		} else {
			List<FriendRel> list = jpaDao
					.findAll(
							FriendRel.class,
							"select f from FriendRel f where (f.user = ? and f.friend = ?) "
									+ "or (f.friend = ? and f.user = ?) and f.isPassed = 1",
							new Object[] { user, home, user, home });

			List<FriendRel> friend = jpaDao
					.findAll(
							FriendRel.class,
							"select f from FriendRel f where (f.user = ? and f.friend = ?) "
									+ "or (f.friend = ? and f.user = ?) and f.isPassed = 0",
							new Object[] { user, home, user, home });
			if (list.size() == 1) {
				return 2;
			} else if (friend.size() == 0) {
				FriendRel f = new FriendRel();
				f.setTime(new Date());
				User u = jpaDao.get(User.class, user.getId());
				User h = jpaDao.get(User.class, home.getId());
				f.setUser(h);
				f.setFriend(u);
				f.setIsPassed(0);
				f.setIsRead(0);
				jpaDao.save(f);
			}
			return 1;
		}
	}

	/**
	 * 获取用户的动态信息
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 用户的动态信息
	 */
	@Override
	public List<DynamicInfo> getUserDynamicInfo(User user, int firstResult,
			int maxResult) {
		Map<String, DynamicInfo> map = new HashMap<String, DynamicInfo>();

		List<Music> musics = jpaDao.findAll(Music.class,
				"from Music m where m.user.id = ?", user.getId());
		for (Music music : musics) {
			DynamicInfo dynamicInfo = new DynamicInfo(music.getUser().getId(),
					music.getUser().getNickname(), music.getUser().getAvatar(),
					music.getId(), music.getTitle(), music.getImage(), null,
					null, music.getPath(), DateConvert
							.convertDateToString(music.getUploadTime()),
					"music", null);
			map.put(DateConvert.convertDateToString(music.getUploadTime()),
					dynamicInfo);
		}

		List<Comment> comments = jpaDao.findAll(Comment.class,
				"from Comment c where c.user.id = ?", user.getId());
		for (Comment comment : comments) {
			DynamicInfo dynamicInfo = new DynamicInfo(
					comment.getUser().getId(), comment.getUser().getNickname(),
					comment.getUser().getAvatar(), comment.getMusic().getId(),
					comment.getMusic().getTitle(), comment.getMusic()
							.getImage(), comment.getMusic().getUser().getId(),
					comment.getMusic().getUser().getNickname(), comment
							.getContent(), DateConvert
							.convertDateToString(comment.getTime()), "comment",
					comment.getMusic().getPath());
			map.put(DateConvert.convertDateToString(comment.getTime()),
					dynamicInfo);
		}

		List<FollowRel> follows = jpaDao.findAll(FollowRel.class,
				"from FollowRel f where f.initiativeFollow.id = ?", user
						.getId());
		for (FollowRel follow : follows) {
			DynamicInfo dynamicInfo = new DynamicInfo(follow
					.getInitiativeFollow().getId(), follow
					.getInitiativeFollow().getNickname(), follow
					.getInitiativeFollow().getAvatar(), follow
					.getPassiveFollow().getId(), follow.getPassiveFollow()
					.getNickname(), null, null, null, null, DateConvert
					.convertDateToString(follow.getTime()), "follow", null);
			map.put(DateConvert.convertDateToString(follow.getTime()),
					dynamicInfo);
		}

		Map<String, DynamicInfo> treeMap = new TreeMap<String, DynamicInfo>(
				new DescComparator());
		treeMap.putAll(map);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>(treeMap.values());

		if (list.size() <= 10) {
			return list;
		}

		return list.subList(firstResult, maxResult);
	}

	/**
	 * 获取用户的动态信息总数
	 * 
	 * @param user 用户
	 * @return 用户的动态信息总数
	 */
	@Override
	public int getTotalUserDynamicInfo(User user) {
		List<Music> musics = jpaDao.findAll(Music.class,
				"from Music m where m.id = ?", user.getId());

		List<Comment> comments = jpaDao.findAll(Comment.class,
				"from Comment c where c.id = ?", user.getId());

		List<FollowRel> follows = jpaDao.findAll(FollowRel.class,
				"from FollowRel f where f.initiativeFollow.id = ?", user
						.getId());

		return musics.size() + comments.size() + follows.size();
	}

	@Override
	public int getTotalMusicsInfo(User user) {
		// TODO Auto-generated method stub
		return jpaDao
		.findAll(
				Music.class,
				"select m from Music m where m.user = ? order by m.uploadTime desc",
				user).size();
	}

	@Override
	public boolean aggreeFriend(Integer id) {
		// TODO Auto-generated method stub
		FriendRel fr = jpaDao.get(FriendRel.class, id);
		fr.setIsPassed(1);
		fr.setIsRead(1);
		jpaDao.update(fr);
		return true;
	}

	@Override
	public boolean refuseFriend(Integer id) {
		// TODO Auto-generated method stub
		FriendRel fr = jpaDao.get(FriendRel.class, id);
		fr.setIsPassed(0);
		fr.setIsRead(1);
		jpaDao.update(fr);
		return true;
	}

}